package com.cloudpivot.project.crmprojectnew.service.impl;

import com.cloudpivot.project.crmprojectnew.configuration.CrmConfig;
import com.cloudpivot.project.crmprojectnew.pojo.contact.Contact;
import com.cloudpivot.project.crmprojectnew.pojo.contact.ContactList;
import com.cloudpivot.project.crmprojectnew.service.ContactService;
import com.cloudpivot.project.crmprojectnew.service.HttpAPIServiceForCrm;
import com.cloudpivot.project.crmprojectnew.service.SystemusersService;
import com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * CRM 联系人档案 实现类
 */
@Service
@Slf4j
public class ContactServiceImpl implements ContactService {

    @Autowired
    private CrmConfig crmConfig;
    @Autowired
    private SystemusersService systemusersService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    private static final String APISTR = "contacts";
    private static final String ENTITYSTR = "contact";

    /**
     * 查询 客户-联系人记录
     *
     * @param guid
     * @return
     */
    @Override
    public Contact getContact(String guid) {
        if ("".equals(guid) || guid == null) {
            return null;
        } else {
            String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
            try {
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl);
                Contact contact = objectMapper.readValue(responseStr, Contact.class);

                return contact;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取客户-联系人列表
     *
     * @param userid
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public ContactList getContacts(String userid, int pageIndex, int pageSize) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            ContactList contactList = new ContactList();
            boolean flag;
            try {
                List<Contact> tmpList = new ArrayList<>();
                String requestUrl = CrmNormalUtil.urlEcode(String.format("%s%s?%s", crmConfig.getCrmurl(), APISTR, CrmNormalUtil.getFetchPage(pageIndex, pageSize, ENTITYSTR)));

                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                contactList = objectMapper.readValue(responseStr, ContactList.class);
                return contactList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取客户联系人列表--单一记录
     *
     * @param userid
     * @param guid
     * @return
     */
    @Override
    public ContactList getContacts(String userid, String guid) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            try {
                ContactList contactList = new ContactList();
                List<Contact> tmpList = new ArrayList<>();
                String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                Contact doEntity = objectMapper.readValue(responseStr, Contact.class);
                tmpList.add(doEntity);

                contactList.setContactList(tmpList);
                contactList.setCount(tmpList.size());
                return contactList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 新增客户联系人
     *
     * @param contact
     * @return
     */
    @Override
    public Contact addContact(Contact contact) {
        String userGuid = "";
        if (contact == null) {
            return null;
        } else {
            try {
                String requestUrl = String.format("%s%s", crmConfig.getCrmurl(), APISTR);
                String responseStr = httpAPIServiceForCrm.doPostCrmSingle(requestUrl, objectMapper.writeValueAsString(contact), userGuid);
                Contact doEntity = objectMapper.readValue(responseStr, Contact.class);
                return doEntity;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 删除客户联系人
     *
     * @param guid
     */
    @Override
    public void delContact(String guid) {
        if (guid != null || !guid.equals("")) {
            String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
            log.info("请求删除url:{}", requestUrl);
            try {
                httpAPIServiceForCrm.doDeleteCrm(requestUrl);
                log.info("删除记录成功");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 修改联系人
     *
     * @param contact
     */
    @Override
    public Contact editContact(Contact contact, String str) {
        Contact requestEntity = contact;
        if (contact == null || contact == null) {
            return null;
        } else {
            try {
                requestEntity.setContactid(null);
                requestEntity.setNew_superior(String.format("/contacts(%s)", requestEntity.get_new_superior_value()));
                requestEntity.set_new_superior_value(null);
                String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, str);
                log.info("请求json" + objectMapper.writeValueAsString(requestEntity));
                String responseStr = httpAPIServiceForCrm.doUpdateCrm(requestUrl, objectMapper.writeValueAsString(requestEntity));
                Contact doEntity = objectMapper.readValue(responseStr, Contact.class);
                return doEntity;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }
}
